<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\WayDB;
use App\DBs\WayUserDB;
use Illuminate\Http\Request;

class WayController extends BaseController
{
    //特效

    protected $db;
    protected $wu;

    public function __construct()
    {
        parent::__construct();
        $this->db = new WayDB();
        $this->wu = new WayUserDB();
    }

    //用户表标识
    public function getStamp(Request $request)
    {
        $stamp = $request->post('stamp',0);
        $obj = new \App\DBs\StampDB();
        $resOne = $obj->getOneByStamp($stamp);
        if (!$resOne) {
            $resDb = $obj->setAdd($stamp);
            if ($resDb['code']!=0) {
                return $this->toAppJson(-2);
            }
        }
        return $this->toAppJson(0);
    }

    //列表
    public function getIndex(Request $request)
    {
        $stamp = $this->getUserStamp();
        if (!$stamp) { return $this->toAppJson(-4); }
        $page = $request->post('page',0);
        $size = $request->post('size',10);
        $pageStart = $page * $size;
        $resDb = $this->db->getIndex($pageStart,$size);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-3);
        }
        $items = $resDb['data']; $total = $resDb['total'];
        return $this->toAppJson(0,$items,$total);
    }

    //一个
    public function getOne(Request $request)
    {
        if (!$this->getUserStamp()) {
            return $this->toAppJson(-4);
        }
        $id = $request->post('id',0);
        $resDb = $this->db->getOne($id);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-3);
        }
        $items = $resDb['data'];
        return $this->toAppJson(0,$items);
    }

    /**
     * 需求列表
     */
    public function getDemandIndex(Request $request)
    {
        $stamp = $this->getUserStamp();
        if (!$stamp) { return $this->toAppJson(-4); }
        $id = $request->post('id',0);
        if (!$id) {
            return $this->toAppJson(-2);
        }
        $resOne = $this->wu->getAppOneNoRender($stamp,$id);
        if ($resOne['code']!=0) {
            $resWay = $this->db->getDemand($id);
            if ($resWay['code']!=0) {
                return $this->toAppJson(-3);
            }
            $items = $resWay['files'];
        } else {
            $items = $resOne['files'];
        }
        return $this->toAppJson(0,$items);
    }

    //GIF
    public function setGif(Request $request)
    {
        $w = $request->post('w',0); $w = ceil($w);
        $h = $request->post('h',0); $h = ceil($h);
        $resOne = $this->db->getOldOne();
        if ($resOne['code']!=0) {
            return $this->toAppJson(-3);
        }
        $id = $resOne['data']['id'];
        $cover = $resOne['data']['cover'];
        $link = $resOne['data']['link'];
        if (file_exists($cover)) {
            return array('code'=>'-2','msg'=>'已存在');
        }
        if (!file_exists($link)) {
            return array('code'=>'-2','msg'=>'地址不存在');
        }
        $ffBase = new \App\Utilitys\FFbase();
        //$resRadio = $ffBase->getVideoRadio($link);
        $resCover = $ffBase->setMp4ToGif($link,$w,$h);
        $resDb = $this->db->setCover($id,$resCover['data']);
        if ($resDb['code']!=0) {
            return array('code'=>'-500','msg'=>'更新封面失败');
        }
        return $this->toAppJson(0);
    }
}